package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.WarehouseOrderInfo;
import com.ruoyi.system.mapper.WarehouseOrderMapper;
import com.ruoyi.system.domain.WarehouseOrder;
import com.ruoyi.system.service.IWarehouseOrderService;

/**
 * 仓库订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-19
 */
@Service
public class WarehouseOrderServiceImpl implements IWarehouseOrderService 
{
    @Autowired
    private WarehouseOrderMapper warehouseOrderMapper;

    /**
     * 查询仓库订单
     * 
     * @param id 仓库订单主键
     * @return 仓库订单
     */
    @Override
    public WarehouseOrder selectWarehouseOrderById(Long id)
    {
        return warehouseOrderMapper.selectWarehouseOrderById(id);
    }

    /**
     * 查询仓库订单列表
     * 
     * @param warehouseOrder 仓库订单
     * @return 仓库订单
     */
    @Override
    public List<WarehouseOrder> selectWarehouseOrderList(WarehouseOrder warehouseOrder)
    {
        return warehouseOrderMapper.selectWarehouseOrderList(warehouseOrder);
    }

    /**
     * 新增仓库订单
     * 
     * @param warehouseOrder 仓库订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWarehouseOrder(WarehouseOrder warehouseOrder)
    {
        String userName = SecurityContextHolder.getUserName();
        warehouseOrder.setCreateOrderUser(userName);
        warehouseOrder.setCreateTime(DateUtils.getNowDate());
        int rows = warehouseOrderMapper.insertWarehouseOrder(warehouseOrder);
        insertWarehouseOrderInfo(warehouseOrder);
        return rows;
    }

    /**
     * 修改仓库订单
     * 
     * @param warehouseOrder 仓库订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWarehouseOrder(WarehouseOrder warehouseOrder)
    {
        warehouseOrderMapper.deleteWarehouseOrderInfoByWarehouseOrderId(warehouseOrder.getId());
        insertWarehouseOrderInfo(warehouseOrder);
        return warehouseOrderMapper.updateWarehouseOrder(warehouseOrder);
    }

    /**
     * 批量删除仓库订单
     * 
     * @param ids 需要删除的仓库订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWarehouseOrderByIds(Long[] ids)
    {
        warehouseOrderMapper.deleteWarehouseOrderInfoByWarehouseOrderIds(ids);
        return warehouseOrderMapper.deleteWarehouseOrderByIds(ids);
    }

    /**
     * 删除仓库订单信息
     * 
     * @param id 仓库订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWarehouseOrderById(Long id)
    {
        warehouseOrderMapper.deleteWarehouseOrderInfoByWarehouseOrderId(id);
        return warehouseOrderMapper.deleteWarehouseOrderById(id);
    }

    /**
     * 新增仓库订单详情信息
     * 
     * @param warehouseOrder 仓库订单对象
     */
    public void insertWarehouseOrderInfo(WarehouseOrder warehouseOrder)
    {
        List<WarehouseOrderInfo> warehouseOrderInfoList = warehouseOrder.getWarehouseOrderInfoList();
        Long id = warehouseOrder.getId();
        if (StringUtils.isNotNull(warehouseOrderInfoList))
        {
            List<WarehouseOrderInfo> list = new ArrayList<WarehouseOrderInfo>();
            for (WarehouseOrderInfo warehouseOrderInfo : warehouseOrderInfoList)
            {
                warehouseOrderInfo.setWarehouseOrderId(id);
                list.add(warehouseOrderInfo);
            }
            if (list.size() > 0)
            {
                warehouseOrderMapper.batchWarehouseOrderInfo(list);
            }
        }
    }
}
